#include "menubutton.h"
#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(const QSize &minSize)
{
    initScene(minSize);
    initResources();
    initEventSystem();
}

void MainWindow::initResources()
{
//    logoInMenu = new ImageItem(":/logo", this->scene(), root, QSize(200, 50));

    qreal unit = this->height() / 20;
    QSize logoSize(12*unit, 4*unit);
    QSize buttonSize(4*unit, unit);
    qreal logoXPos = (-logoSize.width()) / 2;
    qreal buttonXPos = (-buttonSize.width()) / 2;
    qreal yPos = -210;-this->height()/2 + 3*unit;

    logoInMenu = new ImageItem(":/logo", this->scene(), NULL, logoSize);
    logoInMenu->setPos(logoXPos, yPos);
//    qDebug() << logoInMenu->scenePos() << endl;
//    qDebug() << logoInMenu->pos() << endl;

    yPos += (unit+logoSize.height());

    buttonLevel = new MenuButton(":/level-button-normal",
                                 ":/level-button-active",
                                 ":/level-button-highlight",
                                 this->scene(), NULL, buttonSize);
    buttonLevel->setX(buttonXPos);
    buttonLevel->setY(yPos);
//    qDebug() << buttonLevel->scenePos() << endl;
//    qDebug() << buttonLevel->pos() << endl;

    yPos += (unit+buttonSize.height());

    buttonEditor = new MenuButton(":/editor-button-normal",
                                  ":/editor-button-active",
                                  ":/editor-button-highlight",
                                  this->scene(), NULL, buttonSize);
    buttonEditor->setX(buttonXPos);
    buttonEditor->setY(yPos);
//    qDebug() << buttonEditor->scenePos() << endl;
//    qDebug() << buttonEditor->pos() << endl;

    yPos += (unit+buttonSize.height());

    buttonOption = new MenuButton(":/option-button-normal",
                                  ":/option-button-active",
                                  ":/option-button-highlight",
                                  this->scene(), NULL, buttonSize);
    buttonOption->setX(buttonXPos);
    buttonOption->setY(yPos);

    yPos += (unit+buttonSize.height());

    buttonAbout = new MenuButton(":/about-button-normal",
                                  ":/about-button-active",
                                  ":/about-button-highlight",
                                  this->scene(), NULL, buttonSize);
    buttonAbout->setX(buttonXPos);
    buttonAbout->setY(yPos);

    yPos += (unit+buttonSize.height());

    buttonExit = new MenuButton(":/exit-button-normal",
                                  ":/exit-button-active",
                                  ":/exit-button-highlight",
                                  this->scene(), NULL, buttonSize);
    buttonExit->setX(buttonXPos);
    buttonExit->setY(yPos);

}

void MainWindow::initScene(const QSize &minSize)
{
    this->setMinimumSize(minSize);
    qreal width =qreal(minSize.width())-2;
    qreal height = qreal(minSize.height())-2;
    this->setScene(new QGraphicsScene(-width/2, -height/2, width, height));
    this->setMouseTracking(true);
}

void MainWindow::initEventSystem()
{
    connect(buttonLevel, SIGNAL(clicked(MenuButton*)), this, SLOT(menuButtonClicked(MenuButton*)));
    connect(buttonEditor, SIGNAL(clicked(MenuButton*)), this, SLOT(menuButtonClicked(MenuButton*)));
    connect(buttonOption, SIGNAL(clicked(MenuButton*)), this, SLOT(menuButtonClicked(MenuButton*)));
    connect(buttonAbout, SIGNAL(clicked(MenuButton*)), this, SLOT(menuButtonClicked(MenuButton*)));
    connect(buttonExit, SIGNAL(clicked(MenuButton*)), this, SLOT(menuButtonClicked(MenuButton*)));
}

void MainWindow::menuButtonClicked(MenuButton *button)
{
    if (button == buttonExit)
        exit(0);
}

//void MainWindow::mousePressEvent(QMouseEvent *event)
//{
//    qDebug() << "mouse pressed" << endl;
//}

void MainWindow::showMainMenu()
{

}
